/*
 * @lc app=leetcode id=316 lang=cpp
 *
 * [316] Remove:q
 * : Duplicate Letters
 */
class Solution {
public:
    string removeDuplicateLetters(string s) {
        int len = s.length();
        if (len <= 1) {
            return s;
        }
        bool mark[26] = {false};
        char ret[27];
        char c = 'a';
        for (int i=0; i<len; i++) {
            if (isValid(s[i])) {
                mark[s[i]-c] = true;
            }
        }
        int j = 0;
        for (int i=0; i<26;i++) {
            if (mark[i]) {
                ret[j++] = c+i;
            }
        }
        ret[j] = '\0';

        return string(ret);
    }

    bool isValid(char c) {
        if (c >= 'a' && c <= 'z') {
            return true;
        }
        return false;
    }
};

